using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_PortableItem : UIBaseNode
    {
        public static Action<ItemData> onItemSelected { get; set; }

        #region Template Generate,don't modify
        protected class UIB_UIN_Item
        {
            #region ObjectBinding Generate 
            public GameObject commonItem { protected set; get; }
            public ExButton btn { protected set; get; }
            public GameObject check { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("commonItem", out var __tbv0);
                commonItem = __tbv0;
                __binding.TryGetVariableValue<ExButton>("btn", out var __tbv1);
                btn = __tbv1;
                __binding.TryGetVariableValue<GameObject>("check", out var __tbv2);
                check = __tbv2;
            }


            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_OneItem _item;
        #endregion fields

        #region properties

        public ItemData itemData { get; private set; }

        public bool selected
        {
            get => ui.check.activeSelf;
            set => ui.check.SetActive(value);
        }

        protected UIB_UIN_Item ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_Item();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _item = InitChildNode<UIN_OneItem>(ui.commonItem);
            selected = false;
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            ui.btn.AddClickEvent(OnClick);
        }

        protected override void OnDispose()
        {

        }

        #region methonds

        private void OnClick()
        {
            selected = true;
            onItemSelected?.Invoke(itemData);
        }

        public void Refresh(ItemData data, int curItemCid)
        {
            itemData = data;
            selected = curItemCid == data.cid;
            _item.Show(itemData.cfgData);
            _item.SetCount(itemData.num, itemData.num > 1);
        }
        #endregion methonds
    }
}